package com.example.myprojectoffeixue;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

//场景类型界面
public class CJleiXingActivity extends Activity {
    Dialog dialog;
    private MyApplication m;
    private ArrayList<SheBeiClass> sb;
    private ArrayList<SheBeiClass> sb_hw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cjlei_xing);
        info();
    }

    public void info() {
        m = (MyApplication) getApplication();
        sb = new ArrayList<>();
        sb_hw = new ArrayList<>();
        for (int i = 0; i < m.lsb4.size(); i++) {
            SheBeiClass sbc = new SheBeiClass();
            sbc.ieee = m.lsb4.get(i).ieee;
            sbc.port = m.lsb4.get(i).port;
            sbc.status = m.lsb4.get(i).status;
            sbc.deviceId = m.lsb4.get(i).deviceId;
            sbc.mac = m.lsb4.get(i).mac;
            sbc.deviceName=m.lsb4.get(i).deviceName;
            sb.add(sbc);
        }


        for (int i = 0; i < m.lsb2.size(); i++) {
            SheBeiClass sbc = new SheBeiClass();
            sbc.ieee = m.lsb2.get(i).ieee;
            sbc.port = m.lsb2.get(i).port;
            sbc.status = m.lsb2.get(i).status;
            sbc.deviceId = m.lsb2.get(i).deviceId;
            sbc.mac = m.lsb2.get(i).mac;
            sbc.deviceName=m.lsb2.get(i).deviceName;
            sb_hw.add(sbc);
        }

        LinearLayout iv = (LinearLayout) findViewById(R.id.iv_mykaiguan);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        LinearLayout l1 = (LinearLayout) findViewById(R.id.lin1_leixing);//手动
        LinearLayout l2 = (LinearLayout) findViewById(R.id.lin2_leixing);//定时
        LinearLayout l3 = (LinearLayout) findViewById(R.id.lin3_leixing);//门磁
        LinearLayout l4 = (LinearLayout) findViewById(R.id.lin4_leixing);//红外

        l1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddCJActivity.flag=1;
                finish();

            }
        });

        l2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddCJActivity.flag=2;
                finish();

            }
        });

        l3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

      if(m.lsb2.size()==0){
    HttpConnection.showToast(CJleiXingActivity.this,"请先添加红外转发器");
     }else{
                dialog = new Dialog(CJleiXingActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.cjshebei);
                ListView lv = (ListView) dialog.findViewById(R.id.lv_cjshebei);
                lv.setAdapter(new ScenLiandongadapter(sb_hw, CJleiXingActivity.this, 1));
                dialog.show();
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        AddCJActivity.flag=3;
                        AddCJActivity.deviceId=sb_hw.get(position).deviceId;
                        AddCJActivity.ieee=sb_hw.get(position).ieee;
                        AddCJActivity.mac=sb_hw.get(position).mac;
                        AddCJActivity.port=sb_hw.get(position).port;
                        AddCJActivity.deviceName=sb_hw.get(position).deviceName;
                        AddCJActivity.status=sb_hw.get(position).status;
                        dialog.dismiss();
                        finish();
                    }
                });


            }}
        });


        l4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sb.size()==0){
                    HttpConnection.showToast(CJleiXingActivity.this,"请先添加门磁");
                }else{
                dialog = new Dialog(CJleiXingActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.cjshebei);
                TextView tv = (TextView) dialog.findViewById(R.id.tv_cjshebei);
                tv.setText("门磁设备");
                ListView lv = (ListView) dialog.findViewById(R.id.lv_cjshebei);
                lv.setAdapter(new ScenLiandongadapter(sb, CJleiXingActivity.this, 2));
                dialog.show();
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        AddCJActivity.flag=4;
                        AddCJActivity.deviceId=sb.get(position).deviceId;
                        AddCJActivity.ieee=sb.get(position).ieee;
                        AddCJActivity.mac=sb.get(position).mac;
                        AddCJActivity.port=sb.get(position).port;
                        AddCJActivity.status=sb.get(position).status;
                        AddCJActivity.deviceName=sb.get(position).deviceName;
                        dialog.dismiss();
                        finish();


                    }
                });


            }}
        });

    }


}
